﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Warehouse
{
    public partial class Form3 : Form
    {
        string _connectionString;
        DBConnect _db;
        String _username = "";
        String _password = "";
        String _ip = "";
        private Form1 _parentForm;

        public Form3(Form1 parentForm)
        {
            this._parentForm = parentForm;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Boolean _success = false;
            _username = textBox1.Text;
            _password = textBox2.Text;
            _ip = textBox3.Text;
            if (_username != "" && _password != "")
            {
                _db = new DBConnect(_ip, "warehouse_" + _username);
                _connectionString = _db.getDBString();
                using (MySqlConnection _connection = new MySqlConnection(_connectionString))
                {
                    try
                    {
                        _connection.Open();
                        MySqlCommand cmd = _connection.CreateCommand();
                        cmd.CommandText = "SELECT Password FROM password LIMIT 0,1";
                        MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adap.Fill(ds);

                        //buat yg butuh datanya dipisah2 dulu
                        DataTable myDataTable = ds.Tables[0];
                        DataRow tempRow = null;
                        foreach (DataRow tempRow_Variable in myDataTable.Rows)
                        {
                            tempRow = tempRow_Variable;
                            //cara label
                            if (_password == tempRow["Password"].ToString())
                            {
                                _success = true;
                            }
                            else
                            {
                                _success = false;
                                MessageBox.Show("Wrong Password");
                            }
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Wrong Username");
                    }
                    finally
                    {
                        if (_connection.State == ConnectionState.Open)
                        {
                            _connection.Close();
                        }
                    }
                }
            }
            if (_success == true)
            {
                _parentForm.warehouseCabang = _username;
                _parentForm.ipCabang = _ip;
                this.Close();
            }
        }

    }
}
